<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
  
    public function index(){
     // echo"你好";
        if(session('?uid')){
          $this->display();  
        }else{
          $this->display('login');
        }
    }
    public function test(){
        $this->display();
    }

    public function login(){
         $this->display(); 
     }
     public function logout(){
         session('uid',null);
         session('account',null);
         session(null);
         $this->redirect('index');
     }

     public function login_handler(){
         //echo time();
         $account =  I("post.username");
         $password = I("post.password");
         
         $User = M("Admin");
         $condition['account'] = $account;
         $condition['password'] = $password;
         
         //$result = $User->where($condition)->select();
         $result = $User->where($condition)->find();
         /*if($result){
             $this->redirect('index');
         }  else {
             $this->error('用户或密码错误！');
         }*/
         if($result){
             
             $data['login_time'] = time();  //1491362610   vs 1491794166
             
             $User->where("id=".$result['id'])->save($data);
             
             session('uid',$result['id']);
             session('account',$result['account']);
             $this->redirect('index');
         }  else {
             $this->redirect('login');
         }
     }
     public function checkusername(){
         $account = I('post.username');
         $db = M('Admin');
         $condition['account'] = $account;
         $cnt = $db->where($condition)->count();
         if($cnt>0){
             echo 1;
         }else{
             echo 0;
         }
     }
          public function checkpassword(){
         $account = I('post.username');
         $password = I('post.password');
         
         $db = M('Admin');
         $condition['account'] = $account;
         $condition['password'] = $password;
         $cnt = $db->where($condition)->count();
         if($cnt>0){
             echo 1;
         }else{
             echo 0;
         }
     }
    /* public function checkpassword(){
         $account = I('post.uername');
         $password = I('post.password');
         
         $db = M('Admin');
         $condition['account'] = $account;
         $condition['password'] = $password;
         $cnt = $db->where($condition)->count();
         if($cnt>0){
             echo 1;
         }else{
             echo 0;
         }
         
     }*/
     public function checkcode(){
         $code = I('post.code');
         $Verify = new \Think\Verify();
         $result = $Verify->check($code);
         if($result){
             echo 1;
         }else{
             echo 0;
         }
     }

     public function verify_code(){
         $config = array(
             'fontSize' => 25,
             'length'   => 4,
             'imageH'   =>40,
             //'useNoise' => true,  //数字和字母同时存在
             'codeSet'  =>'0123456789'  //纯数字
             
         );
         $Verify = new \Think\Verify($config);
         $Verify->entry();
     }
     
}